package com.kgc.scd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kgc.scd.bean.HotKey;
import com.kgc.scd.bean.SearchHistory;
import com.kgc.scd.mapper.HotKeyMapper;
import com.kgc.scd.service.HotKeyService;
import com.kgc.scd.service.SensitiveFeign;
import com.kgc.scd.util.RedisUtils;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created On : 2022/6/14.
 * <p>
 * Author     : hi
 * <p>
 * Description: 热词搜索
 */
@Slf4j
@RestController
public class HotKeyController {

    @Autowired
    private HotKeyService hotKeyService;

    @Autowired(required = false)
    private HotKeyMapper hotKeyMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private SensitiveFeign sensitiveFeign;

    /**
     * @author : hi
     * @date   : 2022/6/14
     * @param  : 
     * @return : 
     * @description : 首页热词展示
     */
    @GetMapping("/hotSearch")
    public RequestResult<Set<Object>> hotSearch(@RequestParam(value = "hotkeyValue") String hotkeyValue){
         HotKey hotKey =new HotKey();
         String hotkey = "hotKey";
         hotKey.setHotkeyKey(hotkey);
         hotKey.setHotkeyValue(hotkeyValue);

        // 判断redis中是否存在hotkey
        Set<Object> range = redisTemplate.opsForZSet().range(hotkey, 0, -1);
        if (range.size()==0){
            // 查询数据库数据，遍历进redis
            QueryWrapper<HotKey> wrapper = new QueryWrapper<>();
            wrapper.eq("hotkey_key",hotKey.getHotkeyKey());
            List<HotKey> hotKeyList = hotKeyMapper.selectList(wrapper);
            hotKeyList.forEach(hotKey1 -> redisTemplate.opsForZSet().incrementScore(hotkey,hotKey1.getHotkeyValue(),hotKey1.getHotkeyScore()));
        }

        //if (redisUtils.hasKey(hotKey.getHotkeyKey())){
            //redis中有热词
           if (redisTemplate.opsForZSet().rank(hotkey,hotkeyValue) !=null){
               //命中热词，热词数量加一
               int sc = redisTemplate.opsForZSet().incrementScore(hotkey, hotkeyValue, 1).intValue();
               hotKey.setHotkeyScore(sc);
               //数据库score加1
               hotKeyService.addHotKey(hotKey);
           }
        //}
        Set<Object> range1 = redisTemplate.opsForZSet().reverseRange(hotkey, 0, 9);
        System.out.println(range1);
        return ResultBuildUtil.success(range1);
    }


    /**
     * @author : hi
     * @date   : 2022/6/14
     * @param  : [java.lang.String]
     * @return : com.kgc.scd.util.RequestResult<com.kgc.scd.bean.HotKey>
     * @description : 添加热词
     */

    @PostMapping("/addHotKey")
    public RequestResult<HotKey> addHotKey (@RequestParam String hotkeyValue){

        RequestResult<String> stringRequestResult = sensitiveFeign.testSensitiveWord(hotkeyValue);
        String code = stringRequestResult.getCode();
        if(code.equals("500")){
            // 命中敏感词
            return ResultBuildUtil.fail("501","命中敏感词请重新输入");
        }
        HotKey hotKey =new HotKey();
        String hotkey = "hotKey";
        hotKey.setHotkeyKey(hotkey);
        hotKey.setHotkeyValue(hotkeyValue);
        //redis增加热词
        redisTemplate.opsForZSet().add(hotkey,hotkeyValue,0);
        // 数据库增加热词
        hotKeyMapper.insert(hotKey);
        return ResultBuildUtil.success();
    }


    /**
     * @author : hi
     * @date   : 2022/6/14
     * @param  : [java.lang.String]
     * @return : com.kgc.scd.util.RequestResult<com.kgc.scd.bean.HotKey>
     * @description :删除热词
     */

    @PostMapping("/delHotKey")
    public RequestResult<HotKey> delHotKey (@RequestParam String hotkeyValue){
        HotKey hotKey1 =new HotKey();
        String hotkey = "hotKey";
        hotKey1.setHotkeyKey(hotkey);
        hotKey1.setHotkeyValue(hotkeyValue);
        //redis删除热词
        redisTemplate.opsForZSet().remove(hotkey,hotkeyValue);
        // 数据库删除热词
        QueryWrapper<HotKey> wrapper = new QueryWrapper<>();
        wrapper.eq("hotkey_value", hotkeyValue);
        hotKeyMapper.delete(wrapper);
        return ResultBuildUtil.success();
    }

/**
 * @author : hi
 * @date   : 2022/6/14
 * @param  : [java.lang.String]
 * @return : com.kgc.scd.util.RequestResult<com.kgc.scd.bean.HotKey>
 * @description :热词展示
 */
    @PostMapping("/HotKeyList")
     public RequestResult<List<HotKey>> HotKeyList (@RequestParam String HotkeyKey){
        // 判断redis中是否存在hotkey
        Set<Object> range = redisTemplate.opsForZSet().range("hotKey", 0, -1);
        if (range.size()==0){
            // 查询数据库数据，遍历进redis
            QueryWrapper<HotKey> wrapper = new QueryWrapper<>();
            wrapper.eq("hotkey_key","hotKey");
            wrapper.orderByDesc("hotkey_score");
            List<HotKey> hotKeyList = hotKeyMapper.selectList(wrapper);
            hotKeyList.forEach(hotKey1 -> redisTemplate.opsForZSet().incrementScore("hotKey",hotKey1.getHotkeyValue(),hotKey1.getHotkeyScore()));
        }
        QueryWrapper<HotKey> wrapper = new QueryWrapper<>();
        wrapper.eq("hotkey_key",HotkeyKey);
        wrapper.orderByDesc("hotkey_score");
        List<HotKey> hotKeyList = hotKeyMapper.selectList(wrapper);
        return ResultBuildUtil.success(hotKeyList);
    }





    @PostMapping("/tenHotKey")
    public RequestResult<Object> tenHotKey(){
        // 判断redis中是否存在hotkey
        Set<Object> range = redisTemplate.opsForZSet().reverseRange("hotKey", 0, 9);
        if (range.size()==0){
            // 查询数据库数据，遍历进redis
            QueryWrapper<HotKey> wrapper = new QueryWrapper<>();
            wrapper.eq("hotkey_key","hotKey");
            List<HotKey> hotKeyList = hotKeyMapper.selectList(wrapper);
            hotKeyList.forEach(hotKey1 -> redisTemplate.opsForZSet().incrementScore("hotKey",hotKey1.getHotkeyValue(),hotKey1.getHotkeyScore()));
            range = redisTemplate.opsForZSet().reverseRange("hotKey", 0, 9);
        }
        ArrayList<Object> objects = new ArrayList<>();
        objects.addAll(range);

        System.out.println(objects);
        return ResultBuildUtil.success(objects);
    }


}
